1558A - Charmed by the Game - CodeForces Solution


brute force math *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define float double
#define sz 100005
#define all(a) a.begin(), a.end()
#define mod 1000000007
using namespace std;
#define vi vector<int>
#define vvi vector<vector<int>>
#define debug cout << "here" << endl;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define pb push_back
#define ff first
#define ss second
#define pi pair<int, int>
using namespace std;

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cout << vars << " = ";
    string delim = "";
    (..., (cout << delim << values, delim = ", "));
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};


void enumerateSubmasks(int m)
{
    // visits submasks without repetition and in descending order
    for (int s = m;; s = (s - 1) & m)
    {

        if (s == 0)
        {
            //...
            break;
        }
    }
}

int mpow(int a, int b, int m)
{
    if (b == 0)
        return 1;
    int x = mpow(a, b / 2, m);
    x = (x * x) % m;
    if (b % 2)
    {
        x = (x * a) % m;
    }
    return x;
}

void printBinaryString(int n)
{
    vi temp;
    while (n)
    {
        if (n & 1)
            temp.pb(1);
        else
            temp.pb(0);
        n = n >> 1;
    }
    reverse(temp.begin(), temp.end());
    for (auto node : temp)
        cout << node << " ";
    cout << endl;
}

void readVector(vi &a)
{
    int n = a.size();
    rep(i, n) cin >> a[i];
}


int update(int s,int e,int qs,int qe,vi &seg,vi &lazy,int index){
    if(lazy[index]!=-1){
        seg[index] = 1;
        if(s!=e){

        }

        lazy[index] = -1;

    }

    if(qs > e || qe < s) {

    }

    if(s >= qs && e <= qe){

    }

    int mid = (s+e)/2;

    update(s,mid,qs,qe,seg,lazy,2*index);
    update(s,mid,qs,qe,seg,lazy,2*index+1);

}


//vi primes;
//vi primesVisited(sz,-1);
int32_t main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    for(int i = 2 ; i < sz ; i++){
//        if(primesVisited[i]!=-1) continue;
//        primes.pb(i);
//        primesVisited[i] = i;
//        for(int j = i*i ; j < sz ; j+=i) {
//            if(primesVisited[j]==-1) primesVisited[j] = i;
//        }
//    }

    int tc;
    cin >> tc;
    while(tc--){
        int a,b;
        cin >> a >> b;
        // a -> alice, b -> bob
        set<int> ways;
        // 0 1 0 1 0 1...
        // case 1
        int oddPos,evenPos;
        oddPos = (a+b)/2;
        evenPos = ( a+ b - oddPos);

        // ways for alice to cause problem, alice is 0
        rep(i,oddPos + 1){
            // alice wins in these many odd position
            int ao,ae,bo,be;
            ao = i;
            ae = a - i;
            bo = oddPos - ao;
            be = evenPos - ae;

            if((ae) < 0 || bo < 0 || be < 0) continue;

            ways.insert( ( ao + be));
        }


        // ways for alice to cause problem, alice is 1
        rep(i,evenPos + 1){
            // alice wins in these many even position
            int ao,ae,bo,be;
            ae = i;
            ao = a - i;
            bo = oddPos - ao;
            be = evenPos - ae;

            if(ao < 0 || (ae) < 0 || bo < 0 || be < 0) continue;

            ways.insert( ( ae + bo));
        }
        cout << ways.size() << endl;
        for(auto node : ways) cout << node << " ";
        cout << endl;
    }
}


Comments

Submit
0 Comments
More Questions

1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple